package com.muke.service;

import com.muke.common.ComConstants;
import com.muke.dao.SkillGoodRepository;
import com.muke.entity.SkillGood;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@Component
public class SkillGoodService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SkillGoodRepository skillGoodRepository;

    /**
     * 每五秒执行一次 将需要参与秒杀的商品列表加载到内存
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void prepareGood() {
        log.info("开始加载商品");
        //获取所有已经在内存当中的商品ID列表
        Set<Long> set = redisTemplate.boundHashOps(ComConstants.SKILL_GOODS_PHONE).keys();
        List<Long> ids = new ArrayList<>();
        for (Long id : set) {
            ids.add(id);
        }
        List<SkillGood> list = null;
        //只查询出不在内存当中的商品信息，并加载到内存
        if (CollectionUtils.isEmpty(ids)) {
            list = skillGoodRepository.findSkillAll();
        } else {
            list = skillGoodRepository.findSkill(ids);
        }
        if (!CollectionUtils.isEmpty(list)) {
            for (SkillGood skillGood : list) {
                redisTemplate.boundHashOps(ComConstants.SKILL_GOODS_PHONE).put(skillGood.getId(), skillGood);
                //商品队列
                redisTemplate.boundListOps(ComConstants.SKILL_GOODS_QUEUE + skillGood.getId()).leftPushAll(convertoArry(skillGood.getStockCount(),skillGood.getId()));
            }
        }
        // 查看当前缓存中所有的商品信息
        Set keys = redisTemplate.boundHashOps(ComConstants.SKILL_GOODS_PHONE).keys();
        for (Object s : keys) {
            SkillGood skillGood = (SkillGood) redisTemplate.boundHashOps(ComConstants.SKILL_GOODS_PHONE).get(s);
            Long size = redisTemplate.boundListOps(ComConstants.SKILL_GOODS_QUEUE + skillGood.getId()).size();
            log.info(skillGood.getName() + " 库存剩余：" + size);
        }
    }

    private Long[] convertoArry(Integer stockCount, Long id) {
        Long[] idslong = new Long[stockCount];
        for(int i=0;i<stockCount;i++){
            idslong[i] = id;
        }
        return idslong;
    }

    // 提供查询商品信息的方法
    public SkillGood queryProduct(Long productId) {
        return (SkillGood) redisTemplate.boundHashOps(ComConstants.SKILL_GOODS_PHONE).get(productId);
    }

    public void update(SkillGood skillGood) {
        skillGoodRepository.save(skillGood);
    }
}